package com.fenghongzhang.day013;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private Button down;
    private ImageView img;

    private static final String TAG = "MainActivity";
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String s = (String) msg.obj;
            text.setText(s);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.text);
        down = (Button) findViewById(R.id.down);
        img = (ImageView) findViewById(R.id.img);



        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {

                    try {
                        Thread.sleep(1000);
//                        handler.sendMessage(?);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();



        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "run: ");
                try {
//                            URL url = new URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
                    URL url = new URL("http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                            connection.setRequestMethod("GET");
//                            connection.setConnectTimeout();
//                            connection.setReadTimeout();

//                            connection.setDoInput(true);
//                            connection.setDoOutput(true);

//                            if (connection.getResponseCode() == 200){
//                                InputStream inputStream = connection.getInputStream();
//                                StringBuffer stringBuffer = new StringBuffer();
//                                byte[] bytes = new byte[1024];
//                                int len = 0;
//                                while((len= inputStream.read(bytes)) != -1){
//                                    stringBuffer.append(new String(bytes,0,len));
//                                }
//
//                                String string = stringBuffer.toString();
//
//                                Message obtain = Message.obtain();
//                                obtain.obj = string;
//                                handler.sendMessage(obtain);
////                                handler.sendEmptyMessage(1);
////                                handler.sendEmptyMessageDelayed(2,2000);
////                                handler.sendEmptyMessageAtTime(2,System.currentTimeMillis()+1000);
////                                handler.sendMessageDelayed(obtain,2000);
//
//                            }

                    if (connection.getResponseCode() == 200) {
                        InputStream inputStream = connection.getInputStream();
                        final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//                                handler.post(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        img.setImageBitmap(bitmap);
//                                    }
//                                });
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        img.setImageBitmap(bitmap);
//                                    }
//                                });

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();



        Log.i(TAG, "onCreate: ");








        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }
}
